package net.cyue.nest.nestjava.platform.vertx;

import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.ext.web.Router;
import net.cyue.nest.nestjava.common.data.NestApplicationOption;
import net.cyue.nest.nestjava.common.data.http.CORSOption;
import net.cyue.nest.nestjava.common.enums.HTTPStatus;
import net.cyue.nest.nestjava.common.interfaces.http.IRequestHandler;
import net.cyue.nest.nestjava.core.adapter.AbstractHTTPAdapter;

@Deprecated
public class VerticleAdapter
  extends AbstractHTTPAdapter<HttpServer>
{
  private final Vertx vertx = Vertx.vertx();

  public VerticleAdapter() {
    this.httpServer = this.vertx.createHttpServer();
  }

  @Override
  public void use(IRequestHandler handler) {
    Router router = Router.router(this.vertx);
    router.route().handler(context -> {
      final var request = new VerticleHTTPRequest(context.request());
      final var response = new VerticleHTTPResponse(context.response());
      handler.handle(request, response);
    });
    this.httpServer.requestHandler(router);
  }

  @Override
  public void use(String path, IRequestHandler handler) {
    Router router = Router.router(this.vertx);
    router.route(path).handler(context -> {
      final var request = new VerticleHTTPRequest(context.request());
      final var response = new VerticleHTTPResponse(context.response());
      handler.handle(request, response);
    });
    this.httpServer.requestHandler(router);
  }

  @Override
  public void get(IRequestHandler handler) {
    Router router = Router.router(this.vertx);
    router.get().handler(context -> {
      final var request = new VerticleHTTPRequest(context.request());
      final var response = new VerticleHTTPResponse(context.response());
      handler.handle(request, response);
    });
    this.httpServer.requestHandler(router);
  }

  @Override
  public void get(String path, IRequestHandler handler) {
    Router router = Router.router(this.vertx);
    router.get(path).handler(context -> {
      final var request = new VerticleHTTPRequest(context.request());
      final var response = new VerticleHTTPResponse(context.response());
      handler.handle(request, response);
    });
    this.httpServer.requestHandler(router);
  }

  @Override
  public void post(IRequestHandler handler) {
    Router router = Router.router(this.vertx);
    router.post().handler(context -> {
      final var request = new VerticleHTTPRequest(context.request());
      final var response = new VerticleHTTPResponse(context.response());
      handler.handle(request, response);
    });
    this.httpServer.requestHandler(router);
  }

  @Override
  public void post(String path, IRequestHandler handler) {
    Router router = Router.router(this.vertx);
    router.post(path).handler(context -> {
      final var request = new VerticleHTTPRequest(context.request());
      final var response = new VerticleHTTPResponse(context.response());
      handler.handle(request, response);
    });
    this.httpServer.requestHandler(router);
  }

  @Override
  public void head(IRequestHandler handler) {
    Router router = Router.router(this.vertx);
    router.head().handler(context -> {
      final var request = new VerticleHTTPRequest(context.request());
      final var response = new VerticleHTTPResponse(context.response());
      handler.handle(request, response);
    });
    this.httpServer.requestHandler(router);
  }

  @Override
  public void head(String path, IRequestHandler handler) {
    Router router = Router.router(this.vertx);
    router.head(path).handler(context -> {
      final var request = new VerticleHTTPRequest(context.request());
      final var response = new VerticleHTTPResponse(context.response());
      handler.handle(request, response);
    });
    this.httpServer.requestHandler(router);
  }

  @Override
  public void delete(IRequestHandler handler) {
    Router router = Router.router(this.vertx);
    router.delete().handler(context -> {
      final var request = new VerticleHTTPRequest(context.request());
      final var response = new VerticleHTTPResponse(context.response());
      handler.handle(request, response);
    });
    this.httpServer.requestHandler(router);
  }

  @Override
  public void delete(String path, IRequestHandler handler) {
    Router router = Router.router(this.vertx);
    router.delete(path).handler(context -> {
      final var request = new VerticleHTTPRequest(context.request());
      final var response = new VerticleHTTPResponse(context.response());
      handler.handle(request, response);
    });
    this.httpServer.requestHandler(router);
  }

  @Override
  public void put(IRequestHandler handler) {
    Router router = Router.router(this.vertx);
    router.put().handler(context -> {
      final var request = new VerticleHTTPRequest(context.request());
      final var response = new VerticleHTTPResponse(context.response());
      handler.handle(request, response);
    });
    this.httpServer.requestHandler(router);
  }

  @Override
  public void put(String path, IRequestHandler handler) {
    Router router = Router.router(this.vertx);
    router.put(path).handler(context -> {
      final var request = new VerticleHTTPRequest(context.request());
      final var response = new VerticleHTTPResponse(context.response());
      handler.handle(request, response);
    });
    this.httpServer.requestHandler(router);
  }

  @Override
  public void patch(IRequestHandler handler) {
    Router router = Router.router(this.vertx);
    router.patch().handler(context -> {
      final var request = new VerticleHTTPRequest(context.request());
      final var response = new VerticleHTTPResponse(context.response());
      handler.handle(request, response);
    });
    this.httpServer.requestHandler(router);
  }

  @Override
  public void patch(String path, IRequestHandler handler) {
    Router router = Router.router(this.vertx);
    router.patch(path).handler(context -> {
      final var request = new VerticleHTTPRequest(context.request());
      final var response = new VerticleHTTPResponse(context.response());
      handler.handle(request, response);
    });
    this.httpServer.requestHandler(router);
  }

  @Override
  public void all(IRequestHandler handler) {
    Router router = Router.router(this.vertx);
    router.route().handler(context -> {
      final var request = new VerticleHTTPRequest(context.request());
      final var response = new VerticleHTTPResponse(context.response());
      handler.handle(request, response);
    });
    this.httpServer.requestHandler(router);
  }

  @Override
  public void all(String path, IRequestHandler handler) {
    Router router = Router.router(this.vertx);
    router.route(path).handler(context -> {
      final var request = new VerticleHTTPRequest(context.request());
      final var response = new VerticleHTTPResponse(context.response());
      handler.handle(request, response);
    });
    this.httpServer.requestHandler(router);
  }

  @Override
  public void options(IRequestHandler handler) {
    Router router = Router.router(this.vertx);
    router.options().handler(context -> {
      final var request = new VerticleHTTPRequest(context.request());
      final var response = new VerticleHTTPResponse(context.response());
      handler.handle(request, response);
    });
    this.httpServer.requestHandler(router);
  }

  @Override
  public void options(String path, IRequestHandler handler) {
    Router router = Router.router(this.vertx);
    router.options(path).handler(context -> {
      final var request = new VerticleHTTPRequest(context.request());
      final var response = new VerticleHTTPResponse(context.response());
      handler.handle(request, response);
    });
    this.httpServer.requestHandler(router);
  }

  @Override
  public void listen(int port) {
    this.httpServer.listen(port, "0.0.0.0");
  }

  @Override
  public void listen(String port) {
    this.httpServer.listen(Integer.parseInt(port), "0.0.0.0");
  }

  @Override
  public void setErrorHandler(HTTPStatus status, IRequestHandler handler) {
    throw new RuntimeException("暂不支持 setErrorHandler");
  }

  @Override
  public void setNotFoundHandler(IRequestHandler handler) {
    throw new RuntimeException("暂不支持 setNotFoundHandler");
  }

  public HttpServer getInstance() {
    return this.httpServer;
  }

  @Override
  public void initHTTPServer(NestApplicationOption option) {
    final var options = new HttpServerOptions();
    this.httpServer = this.vertx.createHttpServer(options);
  }

  @Override
  public void close() {
    this.httpServer.close();
  }

  @Override
  public void enableCORS(CORSOption option) {
    throw new RuntimeException("暂不支持 enableCORS");
  }
}
